import { FreezePlugin } from '../../../pugins/freeze.plugin';
import { DeleteRowColPlugin } from '../../../pugins/delete-row-col.plugin';
import { OperateBase } from '../../common/operate-base';

export class DeleteRow extends OperateBase {
    protected container: HTMLElement;
    protected plugin: DeleteRowColPlugin
    constructor() {
        super();
    }
    public getDom() {
        this.container = document.createElement('div');
        this.container.setAttribute('class', this.containerClassName);
        this.container.innerHTML=`<span class="z-tools-icon icon iconfont icon-deleterow"></span>`;
        return this.container;
    }
    public selectChange(select: any, cells: any[]): void {
        
    }
    public afterDomInit(): void {
        this.plugin = this.zTableInstance.pluginManager.getPlugin(DeleteRowColPlugin.pluginName);
        this.zTableInstance.getHook().afterSelect.add(() => {
            this.toggleDisabled();
        });

        let iconEle = this.container.querySelector('.icon-deleterow') as HTMLElement;
        iconEle.addEventListener('click', () => {
            if(iconEle.classList.contains('z-table-icon-disabled')) {
                return;
            }
            let rows: number[] = [];
            let select = this.plugin.getSelectInfo();
            for(let row = select.row; row < select.row + select.rowspan; row++) {
                rows.push(row);
            }
            this.plugin.deleteRows(rows);
        });
    }
    protected toggleDisabled() {
        let select = this.plugin.getSelectInfo();
        let iconEle = this.container.querySelector('.icon-deleterow') as HTMLElement;
        if(select && select.row !== -1) {
            iconEle.classList.remove('z-table-icon-disabled');
        } else {
            iconEle.classList.add('z-table-icon-disabled');
        }
    }
}